from django.core.urlresolvers import RegexURLResolver,Resolver404,RegexURLPattern
from django.http import Http404

"""Hack to get view callback string - not possible with normal callback. Works only with Django 0.95 - not with current SVN"""

def resolve_RUP(regexURLPattern, path):
	"""Copy of Django RegexURLPattern.resolve function, returning callback string instead of function"""
	match = regexURLPattern.regex.search(path)
	if match:
		# If there are any named groups, use those as kwargs, ignoring
		# non-named groups. Otherwise, pass all non-named arguments as
		# positional arguments.
		kwargs = match.groupdict()
		if kwargs:
			args = ()
		if not kwargs:
			args = match.groups()
		# In both cases, pass any extra_kwargs as **kwargs.
		kwargs.update(regexURLPattern.default_args)

		return (regexURLPattern.callback,args,kwargs,) #on new version change "callback" with _callback_str




def resolve_RUR(resolver,path):
	"""Copy of Django RegexURLResolver.resolve function. Needs to check, what pattern really is"""
	tried = []
	match = resolver.regex.search(path)
	if match:
		new_path = path[match.end():]
		for pattern in resolver.urlconf_module.urlpatterns:
			try:
				if type(pattern) == RegexURLPattern:
					sub_match = resolve_RUP(pattern,new_path)
				else:
					sub_match = resolve_RUR(pattern,new_path)

			except Resolver404, e:
				tried.extend([(pattern.regex.pattern + '   ' + t) for t in e.args[0]['tried']])
			else:
				if sub_match:
					return sub_match[0], sub_match[1], dict(match.groupdict(), **sub_match[2])
				tried.append(pattern.regex.pattern)
		raise Resolver404, {'tried': tried, 'path': new_path}

def resolve(path,urlconf=None):
	if urlconf is None:
		from django.conf import settings
		urlconf = settings.ROOT_URLCONF
	resolver = RegexURLResolver(r'^/', urlconf)
	return resolve_RUR(resolver,path)
